package com.ce.pms.system.custservice.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.dao.IFrequentEventDao;
import com.ce.pms.system.custservice.model.FrequentEventDO;
import com.ce.pms.system.custservice.vo.FrequentEventVO;
import com.sitechasia.webx.core.support.Page;

/**
 * 描述：测试“频发事件”service
 * @author 王浩羽
 *
 */
public class FrequentEventServiceImplT extends MyJMockBaseTestCase
{
	private FrequentEventServiceImpl serviceImpl;
	private IFrequentEventDao frequentEventDao;
	private FrequentEventDO eventDO;
	private FrequentEventVO eventVO;
	
	//常量数据
	private final Long CURRENT_ID = 2008L;
	private final String CURRENT_NAME = "oldName";
	private final String CURRENT_REMARK = "oldRemark";
	private final String CURRENT_VALIDITY = "0";
	private final short CURRENT_SEQUENCE = (short)1;
	
	private final Long NEW_ID = 2009L;
	private final String NEW_NAME = "newName";
	private final String NEW_REMARK = "newRemark";
	private final String NEW_VALIDITY = "1";
	private final short NEW_SEQUENCE = (short)2;
	
	@Before
	public void setUp() throws Exception
	{
		super.setUp();
		frequentEventDao = context.mock(IFrequentEventDao.class);
		serviceImpl = new FrequentEventServiceImpl();
		serviceImpl.setFrequentEventDao(frequentEventDao);
		serviceImpl.setSysrefcodeDao(sysrefcodeDao);
		
		eventDO = new FrequentEventDO();
		eventVO = new FrequentEventVO();
		
		eventDO.setKeyword(CURRENT_ID);
		eventDO.setName(CURRENT_NAME);
		eventDO.setRemark(CURRENT_REMARK);
		eventDO.setSequence(CURRENT_SEQUENCE);
		eventDO.setValidity(CURRENT_VALIDITY);
		eventDO.setSysInfo(sysInfo);
		
		//VO暂不设置ID，如果VO有ID，使用NEW_ID
		eventVO.setName(NEW_NAME);
		eventVO.setRemark(NEW_REMARK);
		eventVO.setSequence(NEW_SEQUENCE);
		eventVO.setValidity(NEW_VALIDITY);
	}


	@After
	public void tearDown() throws Exception
	{
	}


	@Test
	public void testAddFrequentEvent()
	{
		eventVO.setCreator(SYSTEM_INFO_CREATER);
		final FrequentEventDO eventDO_toAdd = new FrequentEventDO();
		copyData(eventVO, eventDO_toAdd);
		context.checking(new Expectations() {{
			oneOf(sysrefcodeDao).getDate();will(returnValue(SYSTEM_INFO_DATE_CREATED));
			oneOf(frequentEventDao).saveObject(with(any(FrequentEventDO.class))); will(returnValue(eventDO_toAdd));
	    }});
		boolean returnValue = serviceImpl.addFrequentEvent(eventVO);
		context.assertIsSatisfied();
		assertTrue(returnValue);
	}


	@Test
	public void testDeleteFrequentEventByIdFail()
	{
		context.checking(new Expectations() {{
			//保存操作不会执行
			oneOf(frequentEventDao).deleteObjectById(CURRENT_ID);will(throwException(new RuntimeException("被引用"))); 
	    }});
		boolean returnValue = false;
		try
		{
			returnValue = serviceImpl.deleteFrequentEventById(CURRENT_ID);
			fail("it should throw exception!");
		}
		catch(Exception e)
		{
			assertTrue(e instanceof BusinessException);
		}
		context.assertIsSatisfied();
		assertFalse(returnValue);
	}

	@Test
	public void testDeleteFrequentEventByIdSuccess()
	{
		context.checking(new Expectations() {{
			oneOf(frequentEventDao).deleteObjectById(CURRENT_ID);
	    }});
		boolean returnValue = false;
		try
		{
			returnValue = serviceImpl.deleteFrequentEventById(CURRENT_ID);
		}
		catch(Exception e)
		{
			fail("it should not throw exception!");
		}
		context.assertIsSatisfied();
		assertTrue(returnValue);
	}
	
	@Test
	public void testEditFrequentEventFail()
	{
		eventVO.setKeyword(CURRENT_ID);
		eventVO.setModifier(SYSTEM_INFO_NEW_MODIFIER);
		context.checking(new Expectations() {{
			//存在待修改的对象
			oneOf(frequentEventDao).findById(eventVO.getKeyword()); will(returnValue(null));
			never(sysrefcodeDao).getDate();will(returnValue(SYSTEM_INFO_DATE_CREATED));
			//进行修改
			never(frequentEventDao).updateObject(with(any(FrequentEventDO.class)));
	    }});
		boolean returnValue = serviceImpl.editFrequentEvent(eventVO);
		context.assertIsSatisfied();
		assertFalse(returnValue);
	}

	@Test
	public void testEditFrequentEventSuccess()
	{
		eventVO.setKeyword(CURRENT_ID);
		eventVO.setModifier(SYSTEM_INFO_NEW_MODIFIER);
		final FrequentEventDO eventDO_toEdit = new FrequentEventDO();
		copyData(eventDO, eventDO_toEdit);
		context.checking(new Expectations() {{
			//存在待修改的对象
			oneOf(frequentEventDao).findById(eventVO.getKeyword()); will(returnValue(eventDO_toEdit));
			oneOf(sysrefcodeDao).getDate();will(returnValue(SYSTEM_INFO_DATE_CREATED));
			//进行修改
			oneOf(frequentEventDao).updateObject(eventDO_toEdit); will(returnValue(eventDO_toEdit));
	    }});
		boolean returnValue = serviceImpl.editFrequentEvent(eventVO);
		context.assertIsSatisfied();
		assertTrue(returnValue);
		//值已经被修改
		assertEquals(eventVO.getName(), eventDO_toEdit.getName());
		assertEquals(eventVO.getRemark(), eventDO_toEdit.getRemark());
		assertEquals(eventVO.getValidity(), eventDO_toEdit.getValidity());
		assertTrue(eventVO.getSequence()==eventDO_toEdit.getSequence());
	}


	@Test
	public void testQueryAllFrequentEvent()
	{
		//这里只做简单的size测试
		final List<FrequentEventDO> doList_toReturn = new ArrayList<FrequentEventDO>();
		final int LIST_SIZE = 2;
		for(int i = 0; i < LIST_SIZE; i++)
		{
			doList_toReturn.add(new FrequentEventDO());
		}
		context.checking(new Expectations() {{
			oneOf(frequentEventDao).findAll(); will(returnValue(doList_toReturn));
	    }});
		List<FrequentEventVO>  returnValue = serviceImpl.queryAllFrequentEvent();
		context.assertIsSatisfied();
		assertTrue(LIST_SIZE == returnValue.size());
	}


	@Test
	public void testQueryFrequentEventListByPage()
	{
		final SearchVO searchVO = new SearchVO();
		Integer currentPage = 2; 
		Integer pageSize = 5;
		searchVO.setPageNo(currentPage);
		searchVO.setPageSize(pageSize);
		
		final Page page = new Page();
		// expectations
		context.checking(new Expectations() {{
			oneOf(frequentEventDao).queryByPage(searchVO); will(returnValue(page));
	    }});
		Page returnValue = serviceImpl.queryFrequentEventListByPage(searchVO);
		context.assertIsSatisfied();
		assertNotNull(returnValue);
	}

}
